package com.ciwong.epaper.modules.reciteWords.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseLearnAdapter<T> extends RecyclerView.Adapter<BaseLearnViewHolder> {

    protected final Context mContext;
    private final LayoutInflater from;
    protected ArrayList<T> mList = new ArrayList<>();

    public BaseLearnAdapter(Context context) {
        this.mContext = context;
        from = LayoutInflater.from(mContext);
    }

    public void setData(List<T> list) {
        if (mList == null)
            mList = new ArrayList<>();
        if (list != null){
            mList.clear();
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public BaseLearnViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return creatViewHolder(from, parent, viewType);
    }


    @Override
    public void onBindViewHolder(@NonNull BaseLearnViewHolder holder, final int position) {

        binderViewHolder(holder, mList, position);
    }

    protected abstract BaseLearnViewHolder creatViewHolder(LayoutInflater from, ViewGroup parent, int viewType);

    protected abstract void binderViewHolder(BaseLearnViewHolder holder, ArrayList<T> mList, int position);

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

}
